//一些实用接口的集合
#ifndef UTIL_HPP
#define UTIL_HPP
#include <iostream>
#include <sys/stat.h>
#include <thread>
#include <time.h>
namespace blog
{
    namespace util
    {
        class date
        {
        public:
            static time_t now()
            {
                return time(nullptr);
            }
        };
        class file
        {
        public:
            static bool exist(const std::string &pathname)
            {
                struct stat st;
                return (stat(pathname.c_str(), &st) == 0);
            }
            static std::string path(const std::string &pathname)
            {
                if (pathname.empty())
                    return ".";
                size_t pos = pathname.find_last_of("/\\");
                if (pos == std::string::npos)
                    return ".";
                return pathname.substr(0, pos + 1);
            }
            //输入了错误地址无法纠错 纠错功能还未实现
            static void createDirectory(const std::string &pathname)
            {
                // ./abc/bac/ab
                int pos = 0, idx=0;
                std::string curPathname;
                // 逐层查找
                while (idx<pathname.size())
                {
                    pos = pathname.find_first_of("/\\", idx);
                    curPathname = pathname.substr(0, pos);
                    //没有找到/ 说明已经到了文件夹的底部 直接创建文件就行
                    if (pos == std::string::npos)
                    {
                        mkdir(curPathname.c_str(), 0755);
                        break;
                    }
                    if (curPathname == "." || curPathname == "..")
                    {
                        idx=pos+1;
                        continue;
                    }
                    if (pos == idx)
                    {
                        idx = pos + 1;
                        continue;
                    }

                    if(exist(curPathname))
                    {
                        idx=pos+1;
                        continue;
                    }
                    mkdir(curPathname.c_str(),0755);
                    idx=pos+1;
                }
            }
        };
    }
}

#endif